//
//  ChooseWithConditionViewController.m
//  CarUser
//
//  Created by 王森林 on 2018/12/19.
//  Copyright © 2018年 王森林. All rights reserved.
//

#import "ChooseWithConditionViewController.h"
#import "ChooseConditionCell.h"
#import "ChooseResultViewController.h"
@interface ChooseWithConditionViewController ()
@property  (nonatomic, strong) NSArray *bodyAry; //车身选择
@property  (nonatomic, strong) NSArray *seatNumAry; //座数选择
@property  (nonatomic, strong) NSArray *priceAry; //价位选择
@property  (nonatomic, strong) NSArray *energyAry; //能源选择
@property  (nonatomic, strong) NSMutableArray *selectAry;  //选择好的条件
@property  (nonatomic, strong) UILabel *selectLabel; //展示条件label
@property (nonatomic, strong) NSString *jb0000;
@property (nonatomic, strong) NSString *csjg00;
@property (nonatomic, strong) NSString *zdjgmin;
@property (nonatomic, strong) NSString *zdjgmax;
@end

@implementation ChooseWithConditionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    [self fetchDataFromNet];
    self.mainTableView.estimatedRowHeight = 100;
    // Do any additional setup after loading the view.
}

- (void)fetchDataFromNet
{
    //座数
    NSDictionary *params = @{
                             @"FKEY":[CommonMethod createMD5Key:@"DICTIONARIES_ID"],
                             @"DICTIONARIES_ID":@"7408ec459ab74b958a8670cdf0a0e7a1"
                             
                             };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_7 params:params md5Paraname:nil success:^(id  _Nullable response) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        
       
//        self.hotBrandAry  = [resultDict objectForKey:@"list"];
//        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
//        [self.mainTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
         NSDictionary *resultDict  = response;
        NSArray *ary = [resultDict objectForKey:@"list"];
        if (ary != nil && ![ary isKindOfClass:[NSNull class]] && ary.count != 0)
        {
            self.bodyAry = [NSArray arrayWithArray:[resultDict objectForKey:@"list"]];
            NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
            [self.mainTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
       
        }
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    
    
    //车身
    NSDictionary *params1 = @{
                             @"FKEY":[CommonMethod createMD5Key:@"DICTIONARIES_ID"],
                             @"DICTIONARIES_ID":@"ba32f6fea4274c099175fa7a857cfd04"
                             
                             };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_7 params:params1 md5Paraname:nil success:^(id  _Nullable response) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        
        //        NSDictionary *resultDict  = response;
        //        self.hotBrandAry  = [resultDict objectForKey:@"list"];
        //        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
        //        [self.mainTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        NSDictionary *resultDict  = response;
        NSArray *ary = [resultDict objectForKey:@"list"];
        if (ary != nil && ![ary isKindOfClass:[NSNull class]] && ary.count != 0)
        {
            self.seatNumAry = [NSArray arrayWithArray:[resultDict objectForKey:@"list"]];
            NSIndexPath *indexPath=[NSIndexPath indexPathForRow:1 inSection:0];
            [self.mainTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
            
        }
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    

}


- (void)createUI
{
    
    [self.mainTableView registerClass:[ChooseConditionCell class] forCellReuseIdentifier:@"ChooseConditionCell"];
    
    [self createFootView];
    
}

- (void)createFootView
{
    
//    self.mainTableView.backgroundColor = [UIColor blueColor];
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-65-44-64);
        (void)make.left.and.right;
    }];
    UIView *footView = [UIView new];
    footView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:footView];
    [footView mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.left.and.right;
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-64-44);
        make.height.mas_equalTo(65);
    }];
    
    UIButton *resetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [resetBtn setTitle:@"重置" forState:UIControlStateNormal];
    [resetBtn setTitleColor:RGB(50, 50, 50) forState:UIControlStateNormal];
    resetBtn.titleLabel.font = FONT_SIZE(14);
    [resetBtn setBackgroundColor:RGB(255, 218, 68)];
    resetBtn.layer.cornerRadius = 20;
    resetBtn.layer.masksToBounds = YES;
    [resetBtn addTarget:self action:@selector(handleResetAction:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:resetBtn];
    [resetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(footView.mas_left).offset(15);
        make.top.mas_equalTo(footView.mas_top);
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(90);
    }];
    
    
    UIButton *selectNumBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectNumBtn setTitle:@"筛选" forState:UIControlStateNormal];
    [selectNumBtn setTitleColor:RGB(255, 218, 68) forState:UIControlStateNormal];
    selectNumBtn.titleLabel.font = FONT_SIZE(14);
    [selectNumBtn setBackgroundColor:RGB(50, 50, 50)];
    selectNumBtn.layer.cornerRadius = 20;
    selectNumBtn.layer.masksToBounds = YES;
    [selectNumBtn addTarget:self action:@selector(handleSearchAction:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:selectNumBtn];
    [selectNumBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(resetBtn.mas_right).offset(15);
        make.right.mas_equalTo(footView.mas_right).offset(-15);
        make.top.mas_equalTo(footView.mas_top);
        make.height.mas_equalTo(44);
     
    }];
    
    
}

//重置
- (void)handleResetAction:(id)sender
{
    
    [self.selectAry removeAllObjects];
    self.selectLabel.text = @"您选择的条件会显示在这儿";
    
}

//搜索
- (void)handleSearchAction:(id)sender
{
    
    ChooseResultViewController  *controller  = [[ChooseResultViewController alloc] init];
    controller.jb0000 = self.jb0000;
    controller.csjg00 = self.csjg00;
    controller.zdjgmax = self.zdjgmax;
    controller.zdjgmin = self.zdjgmin;
    [self.navigationController pushViewController:controller animated:YES];
    
}


//- (NSArray *)bodyAry
//{
//    if (!_bodyAry) {
//        _bodyAry = @[@"SUV",@"轿车",@"MPV",@"跑车",@"皮卡",@"房车"];
//    }
//    return _bodyAry;
//
//}
//
//- (NSArray *)seatNumAry
//{
//    if (!_seatNumAry) {
//        _seatNumAry = @[@"5门5座",@"3门2座",@"2门4座",@"5门7座",@"4门5座"];
//    }
//    return _seatNumAry;
//
//}

- (NSArray *)priceAry
{
    if (!_priceAry) {
        _priceAry = @[@"20-30万",@"30-50万",@"50-80万",@"80万以上"];
    }
    return _priceAry;
}

//- (NSArray *)energyAry
//{
//    if (!_energyAry) {
//        _energyAry = @[@"手动",@"自动"];
//    }
//    return _energyAry;
//}


- (NSMutableArray *)selectAry
{
    if (!_selectAry) {
        _selectAry = [[NSMutableArray alloc] init];
    }
    return _selectAry;
}

- (UILabel *)selectLabel
{
    if (!_selectLabel) {
        _selectLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, BOUNDWIDTH-15*2, 30.0f)];
        _selectLabel.font = FONT_SIZE(12);
        _selectLabel.textColor = Common_HeadTitle_Color;
        _selectLabel.text = @"您选择的条件会显示在这儿";
     
    }
    return _selectLabel;
    
}

- (void)updateHeadTitle:(id)response
{
    
    NSDictionary *result = response;
    NSString  *opration = [result objectForKey:@"operation"];
    if (opration.intValue == 1) {
        [self.selectAry addObject:[response objectForKey:@"title"]];
    } else {
        [self.selectAry removeObject:[response objectForKey:@"title"]];
        
    }
    NSString *resultString = [self.selectAry  componentsJoinedByString:@"/"];
    self.selectLabel.text = resultString;

    
}


#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, BOUNDWIDTH, 30.0f)];
    headView.backgroundColor = Common_Head_Color;
    [headView addSubview:self.selectLabel];
    
    return headView;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChooseConditionCell *cell = (ChooseConditionCell *)[tableView dequeueReusableCellWithIdentifier:@"ChooseConditionCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    switch (indexPath.row) {
        case 0:
        {
            CWeakSelf;
            [cell seTitleWithName:@"车身" withAry:self.bodyAry withBlock:^(id  _Nonnull response) {
                [weakSelf updateHeadTitle:response];
            }];
        }
            break;
        case 1:
        {
              CWeakSelf;
            [cell seTitleWithName:@"座数" withAry:self.seatNumAry withBlock:^(id  _Nonnull response) {
               [weakSelf updateHeadTitle:response];
            }];
        }
            break;
        case 2:
        {
            CWeakSelf;
            [cell seTitleWithName:@"预算" withAry:self.priceAry withBlock:^(id  _Nonnull response) {
                [weakSelf updateHeadTitle:response];
            }];
        }
            break;
//        case 3:
//        {
//            CWeakSelf;
//            [cell seTitleWithName:@"变速箱" withAry:self.energyAry withBlock:^(id  _Nonnull response) {
//                [weakSelf updateHeadTitle:response];
//            }];
//        }
//            break;
            
        default:
            break;
    }
    return cell;
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
